package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcExpenseItem;
import com.csun.cmny.provider.model.domain.OmcLifeAmbiguity;
import com.csun.cmny.provider.model.dto.levelCare.LevelCareSelectDto;
import com.csun.cmny.provider.model.dto.levelCare.OmcLevelCareSaveDto;
import com.csun.cmny.provider.model.dto.levelCare.OmcLevelCareUpdateDto;
import com.csun.cmny.provider.model.dto.lifeAmbiguity.LifeAmbiguitySaveDto;
import com.csun.cmny.provider.model.vo.levelCare.LevelCareVo;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.BedService;
import com.csun.cmny.provider.service.OmcExpenseItemService;
import com.csun.cmny.provider.service.OmcLevelCareService;
import com.csun.cmny.provider.service.RoomService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/levelCare", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcLevelCareController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcLevelCareService levelCareService;

    @Resource
    private OmcExpenseItemService expenseItemService;

    @Resource
    private RoomService roomService;

    @Resource
    private BedService bedService;


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增护理等级")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addLevelCare(@RequestBody @Valid OmcLevelCareSaveDto levelCareSaveDto, BindingResult br){


        int result=levelCareService.addLevelCare(levelCareSaveDto,getLoginAuthDto());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    @ApiOperation(httpMethod = "POST", value = "删除护理等级")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper deleteLevelCare(Long id){

        //判断是否有使用此护理等级的费用项
        OmcExpenseItem omcExpenseItem = new OmcExpenseItem();
        omcExpenseItem.setLevelCareId(id);
        List<OmcExpenseItem> select = expenseItemService.select(omcExpenseItem);

        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("levelCare.delete.expenItem.error", null));
        }

        //判断是否有房间使用此护理等级
        Room room = new Room();
        room.setLevelCareId(id);
        List<Room> rooms = roomService.selectRooms(room);

        if (PublicUtil.isNotEmpty(rooms)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("levelCare.delete.room.error", null));
        }

        //判断是否有床位使用此护理等级
        Bed bed = new Bed();
        bed.setLevelCareId(id);
        Bed bed1 = bedService.selectByBed(bed);

        if (PublicUtil.isNotEmpty(bed1)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("levelCare.delete.bed.error", null));
        }

        int result=levelCareService.deleteByKey(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改护理等级")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper updateLevelCare(@RequestBody @Valid OmcLevelCareUpdateDto levelCareUpdateDto, BindingResult br){


        int result=levelCareService.updateLevelCare(levelCareUpdateDto,getLoginAuthDto());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    @ApiOperation(httpMethod = "POST", value = "查看本机构护理等级")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper selectLevelCare(@RequestBody LevelCareSelectDto levelCareSelectDto){


        //levelCareSelectDto.setGroupId(getLoginAuthDto().getGroupId());
        List<LevelCareVo>list=levelCareService.selectLevelCare(levelCareSelectDto);

        PageInfo<LevelCareVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }


}
